//
//  LoginInViewController.m
//  ShareNavigation
//
//  Created by 水木 傅 on 12-5-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginInViewController.h"
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequest.h"
#import "SingletonUser.h"

@implementation LoginInViewController
@synthesize UserTextField;
@synthesize PassWordTextField;

//---  接收到请求发回来的消息的函数  ---
//－－－  验证请求  －－－
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
    NSString *ResponseString = [request responseString];
    NSLog(@"%@",ResponseString);
    if([ResponseString isEqualToString:@"TRUE"])
    {
        [SingletonUser shareManager].HasLogin = TRUE;
        [SingletonUser shareManager].UserID = self.UserTextField.text;
        [SingletonUser shareManager].PassWord = self.PassWordTextField.text;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        NSString *msg = @"用户名或密码输入错误";
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"警告"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"返回"
                              otherButtonTitles:nil];
        [alert setTag:0];
        [alert show];
    }
}

-(void)  sendLoginIN
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/~walker/ShareNavigationSVN/trunk/ShareNavigationPHP/LoginIn.php"];
    ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];
    [request setRequestMethod:@"POST"];
    [request setPostValue:UserTextField.text forKey:@"UserID"];
    [request setPostValue:PassWordTextField.text forKey:@"PassWord"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestLoginFinished:)];
    [request setDidFailSelector:@selector(requestLoginFailed:)];
    [request setDefaultResponseEncoding:NSUTF8StringEncoding];
    //---  发送请求信息  ----
    [request startAsynchronous];
    NSLog(@"%@",request.requestMethod);
}

- (void)requestLoginFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",[error localizedFailureReason]);
}    
    
//---  界面上的控件响应事件  ---
- (IBAction)Cancel:(id)sender {
     [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)UserTextEnd:(id)sender {
    [PassWordTextField becomeFirstResponder];
}

- (IBAction)PassWordEnd:(id)sender {
    [self sendLoginIN];
}

- (IBAction)LoginIn:(id)sender {
    [self sendLoginIN];
}


- (void)viewDidUnload {
    [self setUserTextField:nil];
    [self setPassWordTextField:nil];
    [super viewDidUnload];
}
@end
